package com.dhsc.service.impl.yykh.yyzlkh;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhsc.domain.ywzx.yykh.yyzlkh.HfzjVo;
import com.dhsc.domain.ywzx.yykh.yyzlkh.JgfxfbVo;
import com.dhsc.domain.ywzx.yykh.yyzlkh.YchfVo;
import com.dhsc.domain.ywzx.yykh.yyzlkh.YwycclmxVo;
import com.dhsc.domain.ywzx.yykh.yyzlkh.YwzlVo;
import com.dhsc.domain.ywzx.yykh.yyzlkh.YybAvgVo;
import com.dhsc.domain.ywzx.yykh.yyzlkh.YyzlkhpmVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.yykh.yyzlkh.IYyzlkhpmDao;
import com.dhsc.service.yykh.yyzlkh.IYyzlkhpmService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
* @Description: 运营质量考核排名
* @Author: gx
* @Date: 2024/4/22 13:56
*/
@Service
@Slf4j
public class YyzlkhpmServiceImpl implements IYyzlkhpmService {

    @Autowired
    private IYyzlkhpmDao yyzlkhpmDao;

    @Override
    public LayuiTableVo queryYyzlkhpm(YyzlkhpmVo yyzlkhpmVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            List<YyzlkhpmVo> yyzlkhpmVos = yyzlkhpmDao.queryYyzlkhpm(yyzlkhpmVo);
            //需要按照bmqx分组处理
            List<YyzlkhpmVo> list = new ArrayList<>();
            if(CommonUtil.isNotEmptyList(yyzlkhpmVos))
            {
                 for (int i = 0; i < yyzlkhpmVos.size(); i++) {
                     if (i == 0 || (i > 0 && !yyzlkhpmVos.get(i).getBmqx().equals(yyzlkhpmVos.get(i-1).getBmqx()))){
                         YyzlkhpmVo yyzlkhpm = new YyzlkhpmVo();
                         yyzlkhpm.setYyb(yyzlkhpmVos.get(i).getBmqxmc() + "档");
                         yyzlkhpm.setState(1);
                         list.add(yyzlkhpm);
                     }
                     list.add(yyzlkhpmVos.get(i));
                 }
            }
            obj.setMsg("");
            obj.setCount(yyzlkhpmVos.size());
            obj.setData(list);
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询运营质量考核排名信息出错",e);
            throw new MyException("查询运营质量考核排名信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryHfxl(YyzlkhpmVo yyzlkhpmVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(yyzlkhpmVo.getPage(),yyzlkhpmVo.getLimit());
            List<YyzlkhpmVo> list = yyzlkhpmDao.queryHfxl(yyzlkhpmVo);
            PageInfo<YyzlkhpmVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setMsg("");
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询回访效率信息出错",e);
            throw new MyException("查询回访效率信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryHfzj(HfzjVo hfzjVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(hfzjVo.getPage(),hfzjVo.getLimit());
            List<HfzjVo> list = yyzlkhpmDao.queryHfzj(hfzjVo);
            PageInfo<HfzjVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setMsg("");
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询回访质检信息出错",e);
            throw new MyException("查询回访质检信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryYchf(YchfVo ychfVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(ychfVo.getPage(),ychfVo.getLimit());
            List<YchfVo> list = yyzlkhpmDao.queryYchf(ychfVo);
            PageInfo<YchfVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询异常回访信息出错",e);
            throw new MyException("查询异常回访信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryYwyccl(YwycclmxVo ywycclmxVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(ywycclmxVo.getPage(),ywycclmxVo.getLimit());
            List<YwycclmxVo> list = yyzlkhpmDao.queryYwyccl(ywycclmxVo);
            PageInfo<YwycclmxVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询业务异常处理信息出错",e);
            throw new MyException("查询业务异常处理信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryZljg(JgfxfbVo jgfxfbVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(jgfxfbVo.getPage(),jgfxfbVo.getLimit());
            List<JgfxfbVo> list = yyzlkhpmDao.queryZljg(jgfxfbVo);
            PageInfo<JgfxfbVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询配合自律监管措施信息出错",e);
            throw new MyException("查询配合自律监管措施信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryYyfxsj(JgfxfbVo jgfxfbVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(jgfxfbVo.getPage(),jgfxfbVo.getLimit());
            List<JgfxfbVo> list = yyzlkhpmDao.queryYyfxsj(jgfxfbVo);
            PageInfo<JgfxfbVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询运营风险事件信息出错",e);
            throw new MyException("查询运营风险事件信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryFbyygz(JgfxfbVo jgfxfbVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(jgfxfbVo.getPage(),jgfxfbVo.getLimit());
            List<JgfxfbVo> list = yyzlkhpmDao.queryFbyygz(jgfxfbVo);
            PageInfo<JgfxfbVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询非标运营工作信息出错",e);
            throw new MyException("查询非标运营工作信息出错");
        }
        return obj;
    }

    @Override
    public ResponseObject queryYybAvg(YybAvgVo yybAvgVo) {
        ResponseObject obj = new ResponseObject();
        try {
            List<YybAvgVo> list = yyzlkhpmDao.queryYybAvg(yybAvgVo);
            for (int i = 0; i < list.size(); i++) {
                YybAvgVo yybAvg = list.get(i);
                if ("ywblzx_gtyw_xl_data1".equals(yybAvg.getKhlx())){
                    yybAvgVo.setJgycp(yybAvg.getScore());
                }else if ("ywblzx_gtyw_xl_data2".equals(yybAvg.getKhlx())){
                    yybAvgVo.setFzyw(yybAvg.getScore());
                }else if ("ywblzx_gtyw_xl_data3".equals(yybAvg.getKhlx())){
                    yybAvgVo.setYbyw(yybAvg.getScore());
                }
            }
            obj.setData(yybAvgVo);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("查询营业部平均值信息出错",e);
            throw new MyException("查询营业部平均值信息出错");
        }
        return obj;
    }

    @Override
    public ResponseObject queryAllYybAvg(YybAvgVo yybAvgVo) {
        ResponseObject obj = new ResponseObject();
        try {
            List<YybAvgVo> list = yyzlkhpmDao.queryAllYybAvg(yybAvgVo);
            for (int i = 0; i < list.size(); i++) {
                YybAvgVo yybAvg = list.get(i);
                if ("ywblzx_gtyw_xl_hzdata1".equals(yybAvg.getKhlx())){
                    yybAvgVo.setJgycp(yybAvg.getScore());
                }else if ("ywblzx_gtyw_xl_hzdata2".equals(yybAvg.getKhlx())){
                    yybAvgVo.setFzyw(yybAvg.getScore());
                }else if ("ywblzx_gtyw_xl_hzdata3".equals(yybAvg.getKhlx())){
                    yybAvgVo.setYbyw(yybAvg.getScore());
                }
            }
            obj.setData(yybAvgVo);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("查询所有营业部平均值信息出错",e);
            throw new MyException("查询所有营业部平均值信息出错");
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryYwzl(YwzlVo ywzlVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(ywzlVo.getPage(), ywzlVo.getLimit());
            List<YwzlVo> list = yyzlkhpmDao.queryYwzl(ywzlVo);
            PageInfo<YwzlVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setCode(0);
            obj.setCount(pageInfo.getTotal());
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询业务质量信息出错",e);
            throw new MyException("查询业务质量信息出错");
        }
        return obj;
    }
}
